#!/usr/bin/python

import pygame
import pygame.mouse
import Numeric as N
import pygame.surfarray as surfarray
import os
import sys
import Character

# constants
BLOCK_SIZE = 35
DIMENSION_X = 10
DIMENSION_Y = 10
MAP_IMAGE = 'start.jpg'
MAP_CONFIG = 'map.config'
CHAR_IMAGE = 'chargif.gif'
START_COORD_X = 5
START_COORD_Y = 5


# initialize collision detection
RESTRICTED = []
map_config = open( MAP_CONFIG , 'r')
for pair in map_config :
	ele = pair.split()
	ele1 = int(ele[0])
	ele2 = int(ele[1])
	RESTRICTED.append( [ele1, ele2] )

print 'restricted: ' + str(RESTRICTED)
		

pygame.init()

imgsurface = pygame.image.load( MAP_IMAGE )
imgarray = surfarray.array2d(imgsurface)

#screen = pygame.display.set_mode( imgarray.shape[:2], pygame.RESIZABLE, 32 )
screen = pygame.display.set_mode( (DIMENSION_X * BLOCK_SIZE, DIMENSION_Y * BLOCK_SIZE) )

surfarray.blit_array( screen, imgarray )

pygame.display.flip()
pygame.display.set_caption("Thomp RPG")

#char = Character.Character((screen.get_rect().x, screen.get_rect().y))
char = Character.Character((BLOCK_SIZE * START_COORD_X, BLOCK_SIZE * START_COORD_Y ), CHAR_IMAGE )
screen.blit( char.image, char.rect )

#blank = pygame.Surface( (char.rect.width, char.rect.height) )
#blank.set_colorkey((255, 255, 255))

pygame.display.update()

while True :
	for event in pygame.event.get() :
		if event.type == pygame.QUIT:
			sys.exit()

		# movement code
		elif event.type == pygame.KEYDOWN :
			if event.key == pygame.K_LEFT :
				char.update([-BLOCK_SIZE, 0], RESTRICTED, BLOCK_SIZE)
			elif event.key == pygame.K_UP :
				char.update([0, -BLOCK_SIZE], RESTRICTED, BLOCK_SIZE)
			if event.key == pygame.K_RIGHT :
				char.update([BLOCK_SIZE, 0], RESTRICTED, BLOCK_SIZE)
			if event.key == pygame.K_DOWN :
				char.update([0, BLOCK_SIZE], RESTRICTED, BLOCK_SIZE)

		#screen.blit( blank, char.old )
		surfarray.blit_array( screen, imgarray )
		screen.blit( char.image, char.rect )
		pygame.display.update( [char.old, char.rect] )
















